<?php
namespace Home\Controller;
use Think\Controller;
class QuestionController extends CommonController {

    //显示问题列表
    public function QuestionList(){
        $question=M('question');
        $this -> QuestionList = $question
            ->join('course on course.c_id=question.q_c_id')
            ->join('user on user.u_id=question.q_u_id')->select();
        //显示编号
        $cnt = 1;
        $this -> assign('cnt',$cnt);
        $this -> display();
    }

    //显示问题列表
    public function QuestionCourseList($c_id){

        $question=M('question');
        $this -> QuestionCourseList = $question
            ->join('course on course.c_id=question.q_c_id')
            ->join('user on user.u_id=question.q_u_id')
            ->where("question.q_c_id = $c_id")
            ->select();

        //dump($this -> QuestionCourseList);
        //显示编号
//        $cnt = 1;
//        $this -> assign('cnt',$cnt);
        $this -> display();
    }

    //显示问题详情+问题列表
    public function QuestionInfo($q_id){

        $question=M('question');
        $this -> QuestionInfo = $question
            ->join('course on course.c_id=question.q_c_id')
            ->join('user on user.u_id=question.q_u_id')->select($q_id);

       //$this -> display();

        //显示答案列表
        $answer=M('answer');

        $this -> AnswerList = $answer
            ->join('question on question.q_id=answer.an_q_id')
            ->join('user on user.u_id=answer.an_u_id')->where("an_q_id=$q_id")->select();

        //把Q_id记录到session中
        $QuestionId = $q_id;
        session('QuestionId',$QuestionId);
        //dump($QuestionId);

        //dump($this -> QuestionInfo);

        $this -> display();

    }

    //显示提问界面
    public function QuestionAdd()
    {
        $Course = M('Course');
        $courseList = $Course->select();
        $this->assign('courseList',$courseList);

        $this->display();
    }

    //添加问题
    public function doQuestionAdd()
    {
        $question = M("question");

        if (!$question->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            exit($question->getError());
        } else {
            // 创建成功，做其他事情

            if ($question->create()) {
                $pname = 'default.jpg';
                if ($picinfo = $this->uploadQuestion()) {
                    $pname = $picinfo['file']['savename'];
                }
            }
            $question->q_pic = $pname;

            //用session获取用户id
            $systemUserId = session('systemUserId');
            $question->q_u_id = $systemUserId;

            //保存提问时间
            $question->q_time = date("Y-m-d H:i:s",time());


            if ($question->add()) {
                $this->success('添加成功' ,'./questionList', 3);
            } else {
                $this->error("添加失败");
            }
        }

    }

    //显示修改提问界面
    public function QuestionMod($q_id)
    {

        $Course = M('Course');
        $courseList = $Course->select();
        $this->assign('courseList',$courseList);

        $question = M('question');
        $this->QuestionInfo = $question->select($q_id);
        //dump($this->QuestionInfo['0']['q_u_id']);
        //dump(session('systemUserId'));

        //判断该用户是否是提问者
        if ($this->QuestionInfo['0']['q_u_id'] == session('systemUserId')){
            $this->display();
        }else{
            //echo ('您没有权限修改问题！');
            $this->error("您没有权限修改问题!");
        }

    }

    //修改问题
    public function doQuestionMod($q_id)
    {

        $Question = D('Question');

        $QuestionInfo = $Question->select($q_id);

        if (!$Question->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Question->getError());
        } else {
            // 创建成功，做其他事情
            if ($Question->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/questionpics/' . $QuestionInfo['0']['q_pic']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->uploadQuestion()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Question->q_pic = $pname;
                }
                $Question->q_id = $q_id;
                //保存提问时间
                $Question->q_time = date("Y-m-d H:i:s",time());

                if ($Question->save() !== false) {
                    $this->success('修改成功', './questioninfo/q_id/'.$QuestionInfo['0']['q_id'], 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }


    //显示添加答案页面
    public function AnswerAdd($q_id){

        //显示问题详情
        $question=M('question');
        $this -> QuestionInfo = $question
            ->join('course on course.c_id=question.q_c_id')
            ->join('user on user.u_id=question.q_u_id')->select($q_id);

        //记录session
        $QuestionId = $q_id;
        session('QuestionId',$QuestionId);
        //dump($QuestionId);


        $this -> display();
    }


    //添加答案
    public function doAnswerAdd()
    {
        $answer = M("user");

        if (!$answer->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            exit($answer->getError());
        } else {
            // 创建成功，做其他事情
            if ($answer->create()) {
                $pname = 'default.jpg';
                if ($picinfo = $this->uploadAnswer()) {
                    $pname = $picinfo['file']['savename'];
                }
            }
            $answer->an_pic = $pname;

            //用session获取问题id
            $QuestionId = session('QuestionId');
            $answer->an_q_id = $QuestionId;

            //用session获取用户id
            $systemUserId = session('systemUserId');
            $answer->an_u_id = $systemUserId;

            //保存答案时间
            $answer->an_time = date("Y-m-d H:i:s",time());

            if ($answer->add()) {
                $this->success('添加成功' ,'./questioninfo/q_id/'.session('QuestionId'), 3);
            } else {
                $this->error("添加失败");
            }
        }

    }

    //显示修改答案页面
    public function AnswerMod($an_id){

        //显示问题详情
        $question=M('question');

        //用session获取问题id
        $QuestionId = session('QuestionId');

        $this -> QuestionInfo = $question
            ->join('course on course.c_id=question.q_c_id')
            ->join('user on user.u_id=question.q_u_id')->select($QuestionId);

        $answer = M('answer');
        $this->AnswerInfo = $answer->select($an_id);

        //dump($this->AnswerInfo);
        //dump(session('systemUserId'));


        //判断该用户是否是回答者
        if ($this->AnswerInfo['0']['an_u_id'] == session('systemUserId')){
            $this->display();
        }else{
            //echo ('您没有权限修改问题！');
           $this->error("您没有权限修改问题!");
        }

    }


    //修改问题
    public function doAnswerMod($an_id)
    {

        $Answer = D('answer');

        $AnswerInfo = $Answer->select($an_id);

        if (!$Answer->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Answer->getError());
        } else {
            // 创建成功，做其他事情
            if ($Answer->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/answerpics/' . $AnswerInfo['0']['an_pic']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->uploadAnswer()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Answer->an_pic = $pname;
                }
                $Answer->an_id = $an_id;
                //保存提问时间
                $Answer->an_time = date("Y-m-d H:i:s",time());

                if ($Answer->save() !== false) {
                    $this->success('修改成功', './questioninfo/q_id/'.$AnswerInfo['0']['an_q_id'], 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }







    //问题图片 文件上传upload函数
    public function uploadQuestion()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/questionpics/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }
    }

    //答案图片 文件上传upload函数
    public function uploadAnswer()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/answerpics/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }
    }













}